<?php
/**
 * Created by PhpStorm.
 * User: blue
 * Date: 2018/1/27
 * Time: 7:28
 */

namespace app\auth\controller;



use app\common\controller\Base;
use libs\Arr;
use think\Db;
use think\View;

class Group extends Base
{
    protected $modelName="Group";
    protected $pk='id';
    protected $rules;
    public function __construct()
    {
        parent::__construct();
        $rule=Db::table('addons_auth_rule')->field('id,pid,title')->select();
        $rules=(new Arr())->channelLevel($rule,  0, "&nbsp;",  'id');
        $this->rules=$rules;
    }


    public function addC(){
        if($this->request->isPost()){
            //数据处理和验证
            $data=$this->request->param('','','strip_tags,trim');
            $data['rules']=implode(',',$data['rules']);
            $check=$this->validation('\aop\validate\Group',$data,'add');
            if($check['status']==0)$this->error($check['msg']);
            //调用数据模型入库
            $res=$this->model->add($data);
            ($res['status']=='success')?$this->success($res['msg'],'listC'):$this->error($res['msg'],'listC');
        }
        /*显示规则列表*/
        $rule=Db::table('addons_auth_rule')->field('id,pid,title')->select();
        $rules=(new Arr())->channelLevel($rule,  0, "&nbsp;",  'id');
        View::share('rules',$rules);
        return view();
    }

    public function listC(){
        $data=$this->model->show();
        isset($data['data'])&&View::share('data',$data['data']);
        return view();
    }

    public function editC(){
        $id=intval($this->request->param('id'));
        $group=$this->model->get($id);
        View::share(['oldData'=>$group,'rules'=>$this->rules]);
        if($this->request->isPost()){
            $data=$this->request->param('','','strip_tags,trim');
            $data['rules']=implode(',',$data['rules']);
            $check=$this->validation('\aop\validate\Group',$data,'edit');
            if($check['status']==0){
                 $this->error($check['msg']);
             }
            $res=$this->model->edit($data);
             ($res['status']=='success')?$this->success($res['msg'],'listC'):$this->error($res['msg'],'listC');
        }
        return view();
    }

    public function delC(){
        $data=$this->request->param('','','strip_tags');
        $check=$this->validation('\aop\validate\Group',$data,'del');
        if(!$check){
            return json($validate->getError());
        }
        $res=$this->model->del($data['id']);
        return $res['data']?json(['status'=>'success','msg'=>'删除成功']):json(['status'=>'error','msg'=>"删除失败"]);
    }
    //权限分配
    public function assignUser(){
        //查找关联数据
        $id=intval(input('param.id'));
        $member=\app\auth\model\Member::all();
        $myMember=Db::table('addons_auth_group_access')->field('uid')->where('group_id',$id)->select();
        if(!empty($myMember)){
            $myMember=array_column($myMember,'mid');
        }
        View::share(['member'=>$member,'myMember'=>$myMember]);
        if($this->request->post()){
            $data=$this->request->post();
            if(!empty($data)){
                $data=$data['uid'];
                array_walk($data,function(&$value,$key) use($id){
                    $value=['uid'=>$value,'group_id'=>$id];
                });
            }
            Db::startTrans();
            try{
                Db::table('addons_auth_group_access')->where('group_id',$id)->delete();
                empty($data)||Db::table('addons_auth_group_access')->insertAll($data);
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                $this->error('执行失败:'.$e->getMessage());
            }
            $this->success('执行成功','listC');
        }
        //数据入库
        return view();
    }
}